package cn.iocoder.yudao.module.student.controller.app.company;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.module.member.api.company.CompanyApi;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyPageReqDTO;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.job.JobApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -学生端- 企业")
@RestController
@RequestMapping("/student/company")
@Validated
public class StudentCompanyController {

    @Resource
    private CompanyApi companyApi;

    @Resource
    private JobApi jobApi;

    @PostMapping("/page")
    @ApiOperation("获得企业分页")
    public CommonResult<PageResult<CompanyRespDTO>> getCompanyPage(@Valid @RequestBody CompanyPageReqDTO pageVO){
        pageVO.setAuthorizedStatus("authorized");
        pageVO.setStatus("-1");
        pageVO.setSortField(null);
        PageResult<CompanyRespDTO> pageResult = companyApi.getCompanyPage(pageVO);
        return success(pageResult);
    }

    @PostMapping("/page1")
    @ApiOperation("新获得企业")
    public CommonResult<PageResult<CompanyRespDTO>> getCompanyPage1(@Valid @RequestBody CompanyPageReqDTO pageVO){
        pageVO.setAuthorizedStatus("authorized");
        pageVO.setStatus("-1");
        pageVO.setIsNew(1);
        //最新
        pageVO.setPageSize(20);
        List<SortingField> sortList=new ArrayList<>();
        SortingField s=new SortingField();
        s.setField("newWeight");
        s.setOrder("desc");
        sortList.add(s);
        s=new SortingField();
        s.setField("createTime");
        s.setOrder("asc");
        sortList.add(s);
        pageVO.setSortingFields(sortList);
        pageVO.setSortField(null);
        PageResult<CompanyRespDTO> pageResult = companyApi.getCompanyPage(pageVO);
        Long total = pageResult.getTotal();
        PageResult<CompanyRespDTO> newPage=new PageResult<>();
        List<CompanyRespDTO> tList=new ArrayList<>();
        pageVO.setPageSize(50- total.intValue());
        pageVO.setIsNew(null);
        pageVO.setSortingFields(null);
        if(null!=total && total>0){
            List<CompanyRespDTO> list = pageResult.getList();
            tList.addAll(list);
            List<Long> ids = list.stream().map(CompanyRespDTO::getId).collect(Collectors.toList());
            pageVO.setIds(ids);
        }
        pageResult = companyApi.getCompanyPage(pageVO);
        if(null!=pageResult.getTotal() && pageResult.getTotal()>0){
            tList.addAll(pageResult.getList());
        }
        newPage.setList(tList);
        newPage.setTotal(total+pageResult.getTotal());
        return success(newPage);
    }

    @PostMapping("/getHotCompanyPage")
    @ApiOperation("获得热门企业top50")
    public CommonResult<List<CompanyRespDTO>> getHotCompanyPage(@Valid @RequestBody CompanyPageReqDTO pageVO){
        pageVO.setAuthorizedStatus("authorized");
        List<CompanyRespDTO> pageResult = companyApi.getHotCompanyPage(pageVO);
        return success(pageResult);
    }
    @PostMapping("/getHotCompanyPage1")
    @ApiOperation("新获得热门企业top50")
    public CommonResult<List<CompanyRespDTO>> getHotCompanyPage1(@Valid @RequestBody CompanyPageReqDTO pageVO){
        pageVO.setAuthorizedStatus("authorized");
        List<CompanyRespDTO> pageResult = companyApi.getHotCompanyPage1(pageVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @ApiOperation("获得企业")
    public CommonResult<CompanyRespDTO> getCompanyById(@RequestParam("id") Long id){
        CompanyRespDTO pageResult = companyApi.getCompanyById(id);
        Long count=jobApi.getCompanyJobCount(id);
        pageResult.setRecruitmentCount(count.intValue());
        return success(pageResult);
    }

    @GetMapping("/isAuthentication")
    @ApiOperation("获得企业")
    public CommonResult<Boolean> isAuthentication(@RequestParam("phone") String phone){
        return success(companyApi.isAuthentication(phone));
    }

}
